package com.sunk.ytmall.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.sun.org.apache.xpath.internal.operations.Or;
import com.sunk.ytmall.common.Result;
import com.sunk.ytmall.entity.Address;
import com.sunk.ytmall.entity.Goods;
import com.sunk.ytmall.entity.Order;
import com.sunk.ytmall.entity.User;
import com.sunk.ytmall.entity.vo.CartVO;
import com.sunk.ytmall.enums.CartStatusEnum;
import com.sunk.ytmall.service.IAddressService;
import com.sunk.ytmall.service.ICommonInfoService;
import com.sunk.ytmall.service.IGoodsService;
import com.sunk.ytmall.service.IOrderService;
import com.sunk.ytmall.utils.ImgUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
@Transactional
public class OrderController {

    private final Logger logger= LoggerFactory.getLogger(OrderController.class);
    @Autowired
    private IOrderService orderService;
    @Autowired
    private ICommonInfoService commonInfoService;
    @Autowired
    private IAddressService addressService;

    @RequestMapping("/getOrder")
    public Result getOrder(@RequestBody Order order){
        Result result=new Result();
        User u=commonInfoService.getUser();
        Map o=null;
        try {
            o=orderService.selectMap(
                    new EntityWrapper<Order>()
                            .eq("order_id",order.getOrderId())
                            .eq("user_id",u.getUserId())
            );
            o.putAll(addressService.selectMap(
                    new EntityWrapper<Address>()
                    .eq("address_id",o.get("addressId"))
            ));
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("order/getOrder is error.e:"+e.getMessage());
            logger.error("order/getOrder is error.e:"+e.getMessage());
        }

        result.setSuccess(true);
        result.setResult(o);
        return result;
    }

    @RequestMapping("/delOrder")
    public Result delOrder(@RequestBody JSONObject params){
        Result result=new Result();

        return result;
    }

    @RequestMapping("/orderList")
    public Result orderList(){
        Result result=new Result();
        User u=commonInfoService.getUser();
        List<Order> order=orderService.selectList(
                new EntityWrapper<Order>()
                .eq("user_id",u.getUserId())
                .ne("status", CartStatusEnum.CHOOSE.getStatus())
        );
        for (Order o:order) {
            JSONObject json=JSON.parseObject(o.getOrderList());
            List<CartVO> l=JSONObject.parseArray(json.get("order").toString(),CartVO.class);
//            commonInfoService.addUrlImgCart(l);
            json.put("order",l);
            o.setOrderList(JSONObject.toJSONString(json));
        }
        result.setResult(order);
        result.setSuccess(true);
        return result;
    }

}
